home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / e_stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-15  |  3.2 KB  |  118 lines  |  [TEXT/CWIE]

  1. /* e_stat.c */
  2.  
  3. #include <Files.h>
  4. #include <Aliases.h>
  5. #include <errno.h>
  6. #include "sys/unistd.h"
  7. #include "sys/stat.h"
  8.  
  9. #if defined(__MWERKS__) && (__MWERKS__ < 700)
  10. #define ioACUser filler2
  11. #endif
  12.  
  13. int e_stat_guts( const char *path, struct stat *buf, int linkp )
  14. {
  15.   FSSpec fss;
  16.   OSErr err;
  17.   CInfoPBRec cb;
  18.   ParamBlockRec pb;
  19.   Boolean isFolder, wasAliased;
  20.   Str255 fnbuf;
  21.   long cur_dir;
  22.   short cur_vol;
  23.   Str63 vname;
  24.  
  25.   c_to_p( path, fnbuf );
  26.   HGetVol( NULL, &cur_vol, &cur_dir );
  27.   // added to deal with aliases...
  28.   err = FSMakeFSSpec( cur_vol, cur_dir, fnbuf, &fss );
  29.   if( err != noErr
  30.       || (linkp 
  31.           && (ResolveAliasFile( &fss, 1, &isFolder, &wasAliased ) != noErr)))
  32.   { errno = ENOENT; return -1; }
  33.   // get catalog info
  34.   cb.hFileInfo.ioCompletion = nil;
  35.   cb.hFileInfo.ioVRefNum = fss.vRefNum;
  36.   cb.hFileInfo.ioDirID = fss.parID;
  37.   cb.hFileInfo.ioFDirIndex = 0;
  38.   cb.hFileInfo.ioNamePtr = fss.name;
  39.   if( PBGetCatInfoSync(&cb) != 0 )
  40.   { errno = ENOENT; return -1; }
  41.   // get volume info
  42.   pb.volumeParam.ioVolIndex = 0;
  43.   pb.volumeParam.ioVRefNum  = cb.hFileInfo.ioVRefNum;
  44.   pb.volumeParam.ioNamePtr  = vname;
  45.   if( PBGetVInfoSync(&pb) != 0 )
  46.   { errno = ENOENT; return -1; }
  47.   if( buf == NULL ) return 0;
  48.   // make the stat buf
  49.   buf->st_dev     = pb.ioParam.ioVRefNum;
  50.   buf->st_ino     = cb.dirInfo.ioDrDirID;
  51.   buf->st_nlink   = 1;
  52.   buf->st_uid     = getuid();
  53.   buf->st_gid     = getgid();
  54.   buf->st_rdev    = 0;
  55. #if ( __MWERKS__ >= 0x1100 )
  56.   /* seconds between 1/1/1900 and 1/1/1904 */
  57.   buf->st_atime   = cb.hFileInfo.ioFlMdDat + (365L * 4L) * 24L * 60L * 60L;
  58.   buf->st_mtime   = cb.hFileInfo.ioFlMdDat + (365L * 4L) * 24L * 60L * 60L;
  59.   buf->st_ctime   = cb.hFileInfo.ioFlCrDat + (365L * 4L) * 24L * 60L * 60L;
  60. #else
  61.   buf->st_atime   = cb.hFileInfo.ioFlMdDat;
  62.   buf->st_mtime   = cb.hFileInfo.ioFlMdDat;
  63.   buf->st_ctime   = cb.hFileInfo.ioFlCrDat;
  64. #endif
  65.   buf->st_blksize = pb.volumeParam.ioVAlBlkSiz;
  66.   if( cb.dirInfo.ioFlAttrib & 0x10 )
  67.   { // directory
  68.     buf->st_mode = S_IFDIR | 0777;
  69.     //if( cb.dirInfo.ioACUser & 0x04 ) /* locked */
  70.     //if( cb.dirInfo.ioACUser & 0x01 ) /* no "see folders" priv */
  71.     //if( cb.dirInfo.ioACUser & 0x02 ) /* no "see files" priv */
  72.     buf->st_nlink = 2 + cb.dirInfo.ioDrNmFls;
  73.     buf->st_size = cb.dirInfo.ioDrNmFls;
  74.   }
  75.   else
  76.   { // file
  77.     buf->st_nlink = 1;
  78.     if (cb.hFileInfo.ioFlFndrInfo.fdFlags & 0x8000)
  79.     {
  80.       buf->st_mode = S_IFLNK | 0777;
  81.       buf->st_size = buf->st_blksize;
  82.     }
  83.     else
  84.     {
  85.       buf->st_mode = S_IFREG | 0666;
  86.       if( cb.hFileInfo.ioFlAttrib & 0x01 ) // locked
  87.         buf->st_mode &= ~0222; // not writable
  88.       switch (cb.hFileInfo.ioFlFndrInfo.fdType)
  89.       { case 'APPL':
  90.         case 'BINA':
  91.           buf->st_mode |= 0111; // executable
  92.           break;
  93.         default:
  94.           break;
  95.       }
  96.       buf->st_size = cb.hFileInfo.ioFlLgLen; // data fork only
  97.     }
  98.   }
  99.   buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize;
  100.   return 0; /* All is well */
  101. }
  102.  
  103. int e_stat(const char *path, struct stat *buf)
  104. {
  105.   if (path)
  106.     return (e_stat_guts( path, buf, 1 ));
  107.   return (-1);
  108. }
  109.  
  110. int e_lstat(const char *path, struct stat *buf)
  111. {
  112.   if (path)
  113.     return (e_stat_guts( path, buf, 0 ));
  114.   return (-1);
  115. }
  116.  
  117. // end
  118.